package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.Vo.StockProducerV;
import com.khyr.Vo.StockProducerVo;
import com.khyr.dao.ProductDao;
import com.khyr.dao.UserDao;
import com.khyr.entity.Notice;
import com.khyr.entity.StockProducer;
import com.khyr.entity.User;
import com.khyr.fegin.UserFegin;
import com.khyr.service.ProductService;
import com.khyr.vo.R;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @className: ProductServiceImpl
 * @author: Zyl
 * @date: 2025/3/12 15:32
 * @Version: 1.0
 * @description:
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserFegin userFegin;
    @Override
    public R<Page<StockProducer>> findProduct(StockProducerVo stockProducerVo) {
        QueryWrapper<StockProducer> queryWrapper=new QueryWrapper<>();
        // 根据 noticeVo 中的条件动态构建查询条件
        if (StringUtils.hasText(stockProducerVo.getProducterName())) {
            queryWrapper.like("producer_name", stockProducerVo.getProducterName());
        }
        if (StringUtils.hasText(stockProducerVo.getStatus())) {
            queryWrapper.eq("status", stockProducerVo.getStatus());
        }
        if(StringUtils.hasText(stockProducerVo.getProducterCode())){
            queryWrapper.eq("producer_code", stockProducerVo.getProducterCode());
        }
        if(StringUtils.hasText(stockProducerVo.getProducterTel())){
            queryWrapper.like("producer_tel", stockProducerVo.getProducterTel());
        }
        if(StringUtils.hasText(stockProducerVo.getKeywords())){
            queryWrapper.like("keywords", stockProducerVo.getKeywords());
        }
        //时间格式化
        if (stockProducerVo.getDateRange() != null && stockProducerVo.getDateRange().length == 2) {
            queryWrapper.between("create_time", stockProducerVo.getDateRange()[0], stockProducerVo.getDateRange()[1]);
        }
        if(stockProducerVo.getPageNum()!=null&&stockProducerVo.getPageSize()!=null){
            Page<StockProducer> page = new Page<>(stockProducerVo.getPageNum(), stockProducerVo.getPageSize());
            Page<StockProducer> p = productDao.selectPage(page, queryWrapper);
            return new R<>(200,"查询成功",p);
        }else{
            return new R<>(500,"查询失败",null);
        }
    }

    @Override
    public R addProducter(StockProducerV data) {
//        String username = userFegin.getUserInfo(request).getData().getUserName();
//        // 将用户名赋值给notice的createBy字段
//        data.setCreateBy(username);
        int insert = productDao.insert1(data);
        if (insert > 0) {
            return new R<>(200, "添加成功", null);
        } else {
            return new R<>(500, "添加失败", null);
        }

    }

    @Override
    //回显查询
    public R getProducterById(Integer producerId) {
        StockProducer stockProducer = productDao.selectById(producerId);
        if (stockProducer != null) {
            return new R<>(200, "查询成功", stockProducer);
        } else {
            return new R<>(500, "查询失败", null);
        }
    }

    @Override
    public R updateProducter(StockProducerV data) {
        int update = productDao.update1(data);
        if (update > 0) {
            return new R<>(200, "修改成功", null);
        } else {
            return new R<>(500, "修改失败", null);
        }
    }

    @Override
    //删除
    public R deleteProducterByIds(List<Integer> producerIds) {
        int delete = productDao.deleteProducterByIds(producerIds);
        if (delete > 0) {
            return new R<>(200, "删除成功", null);
        } else {
            return new R<>(500, "删除失败", null);
        }

    }

    @Override
    public R<List<StockProducer>> selectAllProducter() {
        QueryWrapper<StockProducer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        List<StockProducer> list = productDao.selectList(queryWrapper);
        if (list != null) {
            return new R<>(200, "查询成功", list);
        } else {
            return new R<>(500, "查询失败", null);
        }
    }
}
